package gui_layer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.MenuBarUI;

@SuppressWarnings("serial")
public class MainWindow extends JFrame
{

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JDesktopPane desktopPane;
	private int privilage_level;
	

	/**
	 * Create the frame.
	 */
	public MainWindow()
	{
		setTitle("Techno-mag warehouse and sale manager");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try
		{
			setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{
		}
		setBounds(50, 50, 1200, 600);
		privilage_level = CurrentAccount.get_privilage_level();
		menuBar = new JMenuBar();
		menuBar.setUI ( new MenuBarUI()
	    {
	        public void paint ( Graphics g, JComponent c )
	        {
	            g.setColor (Color.getHSBColor(312, 0.11f, 0.17f));
	            g.fillRect ( 0, 0, c.getWidth (), c.getHeight () );
	        }
	    } );
		
		
		setJMenuBar(menuBar);
		try
		{
			final float[] values = Color.RGBtoHSB(255, 104, 0, null);
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			UIManager.put("Menu.background", Color.getHSBColor(312, 0.11f, 0.17f));
            UIManager.put("Menu.foreground", Color.WHITE);
            UIManager.put("Menu.selectionBackground", Color.getHSBColor(values[0], values[1], values[2]));
            UIManager.put("Menu.selectionForeground", Color.WHITE);
            UIManager.put("Menu.opaque", true);
            UIManager.put("MenuItem.background",Color.getHSBColor(312, 0.11f, 0.17f));
            UIManager.put("MenuItem.foreground", Color.WHITE);
            UIManager.put("MenuItem.selectionBackground", Color.getHSBColor(values[0], values[1], values[2]));
            UIManager.put("MenuItem.selectionForeground", Color.WHITE);
            UIManager.put("MenuItem.opaque", true);
			deal_with_sales();
			if(privilage_level != 0)
			{
				deal_with_products();
				deal_with_restocks();
			}
			deal_with_customers();
			deal_with_accounts();
			deal_with_about();
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e) {}

		contentPane = new ImagePanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		desktopPane = new JDesktopPane();
		contentPane.add(desktopPane, BorderLayout.CENTER);
		desktopPane.setOpaque(false);
	}

	private void deal_with_sales()
	{
		
		JMenu mnSale = new CustomMenu("Sale");
		menuBar.add(mnSale);


		JMenuItem mntmNewSale = new JMenuItem("New Sale");
		mntmNewSale.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				NewSaleGUI ns_gui = new NewSaleGUI(null);

				desktopPane.add(ns_gui);
				locate_on_screen(ns_gui);
				ns_gui.setVisible(true);

			}
		});
		mnSale.add(mntmNewSale);
		if(privilage_level != 0)
		{
			JMenuItem mntmManageSales = new JMenuItem("Manage Sales");
			mntmManageSales.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
						ManageSaleGUI ms_gui = new ManageSaleGUI();

						desktopPane.add(ms_gui);
						locate_on_screen(ms_gui);
						ms_gui.setVisible(true);

				}
			});
			mnSale.add(mntmManageSales);
		}
	}

	private void deal_with_products()
	{
		JMenu mnProducts = new CustomMenu("Products");

		menuBar.add(mnProducts);

		JMenuItem mntmNewProduct = new JMenuItem("New Product");
		mntmNewProduct.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
					NewProductGUI np_gui = new NewProductGUI(null, false);

					desktopPane.add(np_gui);
					locate_on_screen(np_gui);
					np_gui.setVisible(true);
			}
		});
		mnProducts.add(mntmNewProduct);

		JMenuItem mntmManageProducts = new JMenuItem("Manage Products");
		mntmManageProducts.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
					ManageProductsGUI mp_gui = new ManageProductsGUI();

					desktopPane.add(mp_gui);
					locate_on_screen(mp_gui);
					mp_gui.setVisible(true);
			}
		});
		mnProducts.add(mntmManageProducts);
	}

	private void deal_with_restocks()
	{
		JMenu mnRestock = new CustomMenu("Restock");

		menuBar.add(mnRestock);

		JMenuItem mntmNewRestock = new JMenuItem("New Restock");
		mntmNewRestock.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				NewRestockGUI nr_gui = new NewRestockGUI(null);
				desktopPane.add(nr_gui);
				locate_on_screen(nr_gui);
				nr_gui.setVisible(true);
			}
		});
		mnRestock.add(mntmNewRestock);

		JMenuItem mntmManageRestocks = new JMenuItem("Manage Restocks");
		mntmManageRestocks.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				ManageRestockGUI mr_gui = new ManageRestockGUI();
				desktopPane.add(mr_gui);
				locate_on_screen(mr_gui);
				mr_gui.setVisible(true);
			}
		});
		mnRestock.add(mntmManageRestocks);

        JMenuItem mntmNewSupplier = new JMenuItem("New Supplier");
		mntmNewSupplier.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				NewSupplierGUI sp_gui = new NewSupplierGUI(null, true);
				desktopPane.add(sp_gui);
				locate_on_screen(sp_gui);
				sp_gui.setVisible(true);
			}
		});
		mnRestock.add(mntmNewSupplier);

		JMenuItem mntmManageSuppliers = new JMenuItem("Manage Suppliers");
		mntmManageSuppliers.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				ManageSupplierGUI sp_gui = new ManageSupplierGUI();
				locate_on_screen(sp_gui);
				desktopPane.add(sp_gui);
				sp_gui.setVisible(true);
			}
		});
		mnRestock.add(mntmManageSuppliers);
	}

	private void deal_with_customers()
	{
		JMenu mnCustomers = new CustomMenu("Customers");

		menuBar.add(mnCustomers);

		JMenuItem mntmNewCustomer = new JMenuItem("New Customer");
		mntmNewCustomer.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
					NewCustomerGUI nc_gui = new NewCustomerGUI(null, false);

					desktopPane.add(nc_gui);
					locate_on_screen(nc_gui);
					nc_gui.setVisible(true);
			}
		});
		mnCustomers.add(mntmNewCustomer);

		if(privilage_level != 0)
		{
			JMenuItem mntmManageCustomers = new JMenuItem("Manage Customers");
			mntmManageCustomers.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
						ManageCustomersGUI mc_gui = new ManageCustomersGUI();

						desktopPane.add(mc_gui);
						locate_on_screen(mc_gui);
						mc_gui.setVisible(true);
				}
			});
			mnCustomers.add(mntmManageCustomers);
		}
	}

	private void deal_with_accounts()
	{
		JMenu mnAccounts = new CustomMenu("Accounts");

		menuBar.add(mnAccounts);

		JMenuItem mntmMyAccount = new JMenuItem("My Account");
		mntmMyAccount.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
					NewAccountGUI ma_gui = new NewAccountGUI(CurrentAccount.get_instance().get_current_account());

					desktopPane.add(ma_gui);
					locate_on_screen(ma_gui);
					ma_gui.setVisible(true);
			}
		});
		mnAccounts.add(mntmMyAccount);

		if(privilage_level != 0)
		{
			JMenuItem mntmNewAccount = new JMenuItem("New Account");
			mntmNewAccount.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
						NewAccountGUI na_gui = new NewAccountGUI(null);

						desktopPane.add(na_gui);
						locate_on_screen(na_gui);
						na_gui.setVisible(true);
				}
			});
			mnAccounts.add(mntmNewAccount);

			JMenuItem mntmManageAccounts = new JMenuItem("Manage Accounts");
			mntmManageAccounts.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
						ManageAccountsGUI ma_gui = new ManageAccountsGUI();

						desktopPane.add(ma_gui);
						locate_on_screen(ma_gui);
						ma_gui.setVisible(true);
				}
			});
			mnAccounts.add(mntmManageAccounts);
		}

		JMenuItem mntmLogOut = new JMenuItem("Log out");
		mntmLogOut.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
					if(JOptionPane.showConfirmDialog(desktopPane, "Are you sure you want to log out?",
							"Log out", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
					{
						LoginWindow lw = new LoginWindow();
						CurrentAccount.get_instance().set_current_account(null);
						lw.setVisible(true);
						dispose();
					}
			}
		});
		mnAccounts.add(mntmLogOut);
	}

	private void deal_with_about()
	{
		JMenu mnAbout = new CustomMenu("Help");

		menuBar.add(mnAbout);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JOptionPane.showMessageDialog(desktopPane, "Techno-Mag warehouse and sale management system V 1.0", "About", JOptionPane.PLAIN_MESSAGE);
			}
		});
		mnAbout.add(mntmAbout);
	}

	private void locate_on_screen(JInternalFrame gui)
	{
		JInternalFrame last_frame = desktopPane.getSelectedFrame();
		if(last_frame != null)
		{
			Point position = last_frame.getLocation();
			gui.setLocation((int) position.getX() + 15, (int) position.getY() + 15);
		}
	}
	
	
	class CustomMenu extends JMenu
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 5957413969557750931L;

		public CustomMenu(String text)
		{
			super(text);
			final float[] values = Color.RGBtoHSB(255, 104, 0, null);
			setRolloverEnabled(true);
			setForeground(Color.WHITE);
			setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
			setOpaque(true);
			getPopupMenu().setBorder(new EmptyBorder(1, 1, 1, 1));
			this.addMouseListener(new MouseAdapter()
			{
				@Override
				public void mouseExited(MouseEvent arg0)
				{
					setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
				}
				
				@Override
				public void mouseEntered(MouseEvent arg0)
				{
					setBackground(Color.getHSBColor(values[0], values[1], values[2]));
				}

			});
		}
	}
}
